from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
import cgi
import feedparser
COMMON_CHANNEL_PROPERTIES = [
    ('Channel title:', 'title', None),
#    ('Sub title:', 'subtitle', None),
    ('Channel description:', 'description', 1000),
    ('Channel URL:', 'link', None),
]
COMMON_ITEM_PROPERTIES = [
    ('Item title:', 'title', None),
    ('Item description:', 'description', 1000),
    ('Item URL:', 'link', None),
]
INDENT = u'<p>'
class MainPage(webapp.RequestHandler):
    
    
    def get(self):
        self.response.headers ['Content-Type'] = 'text/html;charset=utf-8' 
        #self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(''' <html>
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title> 试试看 </title> 
        </ head><body>''')
        self.response.out.write('Hello, webapp World!<BR>')        
        feed_data = feedparser.parse("http://feed.feedsky.com/caozenghui")
        channel, items = feed_data.feed, feed_data.entries
#        str=channel['title']
#        str=str.encode('utf-8')
        #Display core feed data
        for label, prop, trunc in COMMON_CHANNEL_PROPERTIES:
            value = channel[prop]
            if trunc:
                value = value[:trunc] + u'...'
            str = label + value
            str = str.encode('utf-8')
            self.response.out.write(str)
        
        self.response.out.write("Feed items:")
        for item in items:
            for label, prop, trunc in COMMON_ITEM_PROPERTIES:
                value = item[prop]
                if trunc:
                    value = value[:trunc] + u'...'
                str=INDENT+label+value
                str = str.encode('utf-8')
                self.response.out.write(str)
            #self.response.out.write(INDENT+u'---')   
            
        self.response.out.write(''' </body></html> ''')

application = webapp.WSGIApplication([('/', MainPage)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
